package com.putong35.easyui.search;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.putong35.easyui.R;

/**
 * 搜索结果界面
 *
 * @Author: ${USER}
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: ${DATE} ${TIME}
 * @Version: 1.0.0
 */
public class PromptFragment extends Fragment implements OnSearchKeyChangedListener {
    boolean shouldShowData = false;
    Button btnChose;

    public PromptFragment() {
        // Required empty public constructor
    }

    public static PromptFragment newInstance(String param1, String param2) {
        PromptFragment fragment = new PromptFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.search_fragment_prompt, container, false);
        btnChose = (Button) view.findViewById(R.id.btnChose);
        btnChose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onPromptSelected("prompt test");

            }
        });
        return view;
    }

    /**
     * 输入框中的用户数据改变了
     *
     * @param s
     */
    public void onTextChanged(CharSequence s) {
        Log.e("pww", "onTextChanged " + shouldShowData);
        if (TextUtils.isEmpty(s)) {
            shouldShowData = false;
            getActivity().getSupportFragmentManager().beginTransaction().hide(this).commitNowAllowingStateLoss();
        } else {
            shouldShowData = true;
            // do something load data operation
            new View(getActivity()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    Log.e("pww", "run: postDelayed " + shouldShowData);
                    if (shouldShowData) {
                        getActivity().getSupportFragmentManager().beginTransaction().show(PromptFragment.this).commitAllowingStateLoss();
                    }

                }
            }, 2000);

        }

    }


    void onPromptSelected(String prompt) {
        if (getActivity() instanceof OnSearchKeyChangedListener) {
            OnSearchKeyChangedListener onSearchKeyChanged = (OnSearchKeyChangedListener) getActivity();
            onSearchKeyChanged.onSearchKeyChanged(prompt);
        }
    }

    @Override
    public void onSearchKeyChanged(String newSearchKey) {
        shouldShowData = false;
        if (this.isVisible()) {
            getActivity().getSupportFragmentManager().beginTransaction().hide(this).commitAllowingStateLoss();
        }
    }
}
